package com.aoxun.android.gridmanager.common;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.aoxun.android.commons.base.BaseActivity;
import com.aoxun.android.gridmanager.R;

import com.just.library.AgentWeb;
import com.socks.library.KLog;

public class WebviewActivity extends BaseActivity {
    private AgentWeb mAgent;
    private WebView mWebView;
    private LinearLayout llContainer;

    private String urlString;

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public void initView() {
        urlString = getIntent().getStringExtra("urlString");

        if(urlString == null){
            return;
        }

        mAgent = AgentWeb.with(this)
                .setAgentWebParent(llContainer,new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT)
                )
                .useDefaultIndicator()
                .defaultProgressBarColor()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                //.additionalHttpHeader(k,v)  //添加头
                .setSecutityType(AgentWeb.SecurityType.strict)
                .createAgentWeb()//
                .ready()
                .go(urlString);
        mWebView = mAgent.getWebCreator().get();

        //刷新
        //mAgent.getLoader().loadUrl(urlString);
        //syncCookie
        //AgentWebConfig.syncCookie("http://www.jd.com","ID=XXXX")

        mAgent.getJsInterfaceHolder().addJavaObject("native",new AndroidInterface(mAgent,this));
    }



    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_webview;
    }

    private WebViewClient mWebViewClient = new WebViewClient(){
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            KLog.e("Info","BaseWebActivity onPageStarted");
        }
    };

    private WebChromeClient mWebChromeClient = new WebChromeClient(){
        @Override
        public void onProgressChanged(final WebView webView, int i) {
            super.onProgressChanged(webView, i);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgent.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgent.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgent.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgent.destroy();
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        mAgent.uploadFileResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    public static class  AndroidInterface{
        private AgentWeb agentWeb;
        private WebviewActivity activity;
        public AndroidInterface(AgentWeb agentWeb, WebviewActivity activity){
            this.agentWeb = agentWeb;
            this.activity = activity;
        }

        @JavascriptInterface
        public void finish(){
            activity.finish();
        }

        @JavascriptInterface
        public void goToAcitity(String className, String jsonData){
            Intent intent = new Intent();
            intent.setClassName(activity, className);

            if(!TextUtils.isEmpty(jsonData))
                intent.putExtra("data", jsonData);

            activity.startActivity(intent);
        }

        @JavascriptInterface
        public void reflesh(){
            agentWeb.getLoader().loadUrl(activity.urlString);
        }

    }
}
